package com.tzl.design_pattern.singleton_pattern;

/**
 * 懒汉式
 *
 * @author Zhilei Tang
 * @date Created in 2020/11/15 13:42
 */
public class LazySingleton {

  private static volatile LazySingleton instance; // volatile关键字,防止指令重排序,

  private LazySingleton() {}

  /**
   * 双检锁,保证多线程情况下的线程安全
   *
   * @return
   */
  public static LazySingleton getInstance() {
    if (instance == null) {
      synchronized (LazySingleton.class) {
        if (instance == null) {
          instance = new LazySingleton();
        }
      }
    }
    return instance;
  }
}
